package cn.erbudev.model;

import cn.erbudev.util.DrawUtil;

import java.util.ArrayList;
import java.util.List;

public class Line extends DrawEntity {

    private double denominator = Double.MAX_VALUE;  //斜率
    private double intercept = Double.MAX_VALUE;    //截距
    private Point direction;

    public Line(Point startPoint, Point endPoint) {
        this.startPoint = startPoint;
        this.endPoint = endPoint;
        double absX = startPoint.getX() - endPoint.getX();
        if (Math.abs(absX) > DrawUtil.Error) {
            this.denominator = (startPoint.getY() - endPoint.getY()) / absX;
            this.intercept = (startPoint.getX() * endPoint.getY() - startPoint.getY() * endPoint.getX()) / absX;
        }
        this.direction = new Point(endPoint.getX() - startPoint.getX(), endPoint.getY() - startPoint.getY());
    }

    public Point getStartPoint() {
        return startPoint;
    }

    public void setStartPoint(Point startPoint) {
        this.startPoint = startPoint;
    }

    public Point getEndPoint() {
        return endPoint;
    }

    public void setEndPoint(Point endPoint) {
        this.endPoint = endPoint;
    }

    public double getDenominator() {
        return denominator;
    }

    public void setDenominator(double denominator) {
        this.denominator = denominator;
    }

    public Point getDirection() {
        return direction;
    }

    public void setDirection(Point direction) {
        this.direction = direction;
    }

    public double getIntercept() {
        return intercept;
    }

    public void setIntercept(double intercept) {
        this.intercept = intercept;
    }

    @Override
    public DrawEntity reduce(Point point, boolean flag) {
        Line arc = flag ? new Line(point, endPoint) : new Line(startPoint, endPoint);
        arc.setID(arc.getID());
        return arc;
    }

    @Override
    public double getEntityLength() {
        return startPoint.getDistance(endPoint);
    }

    @Override
    public List<Point> getIntersection(DrawEntity other) {
        switch (other.getType()) {
            case DrawEntity.LINE:
                return DrawUtil.getLine2LineCrossPoint(this, (Line) other);
            case DrawEntity.ARC:
                return DrawUtil.getLine2ArcCrossPoint(this, (Arc) other);
            default:
                return new ArrayList<>();
        }
    }

    @Override
    public List<Point> getChildCrossPoints(DrawEntity other, double d) {
        switch (other.getType()) {
            case DrawEntity.LINE:
                return DrawUtil.getLineIntersection(this, (Line) other, d);
            case DrawEntity.ARC:
                return DrawUtil.getLine2ArcIntersection(this, (Arc) other, d);
            default:
                return new ArrayList<>();
        }
    }

    @Override
    public int getType() {
        return DrawEntity.LINE;
    }

    @Override
    public String toString() {
        return "Line{" +
                "startPoint=" + startPoint +
                ", endPoint=" + endPoint +
                ", denominator=" + denominator +
                ", intercept=" + intercept +
                ", direction=" + direction +
                '}';
    }
}
